﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows.Forms;

namespace GDFlacTool.Forms
{
    public partial class FormTitleReplace : Form
    {
        private readonly List<string> _tags = new List<string>();
        private string _searchInTag = string.Empty;
        private string _replaceInTag = string.Empty;
        private bool _ignoreCase;
        private readonly StringCollection _searchCollection;
        private readonly StringCollection _replaceCollection;

        public FormTitleReplace()
        {
            InitializeComponent();

            _searchCollection = Properties.Settings.Default.SearchCollection;
            _replaceCollection = Properties.Settings.Default.ReplaceCollection;
        }

        private void LoadComboBoxes()
        {
            cboSearch.DataSource = _searchCollection;
            cboReplace.DataSource = _replaceCollection;
        }

        private void SaveComboBoxes()
        {
            if (!_searchCollection.Contains(cboSearch.Text))
            {
                if (_searchCollection.Count == 15)
                {
                    _searchCollection.RemoveAt(14);
                }

                _searchCollection.Insert(0, cboSearch.Text);
            }

            if (!_replaceCollection.Contains(cboReplace.Text))
            {
                if (_replaceCollection.Count == 15)
                {
                    _replaceCollection.RemoveAt(14);
                }

                _replaceCollection.Insert(0, cboReplace.Text);
            }
        }

        public List<string> Tags
        {
            get { return _tags; }
        }

        public string SearchInTag
        {
            get { return _searchInTag; }
        }

        public string ReplaceInTag
        {
            get { return _replaceInTag; }
        }

        public bool IgnoreCase
        {
            get { return _ignoreCase; }
        }

        private void cboSearch_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cboSearch.Text))
            {
                btnReplace.Enabled = false;
            }
            else
            {
                btnReplace.Enabled = true;
            }
        }

        private void FormTitleReplace_Load(object sender, EventArgs e)
        {
            LoadComboBoxes();

            if (Properties.Settings.Default.SearchCollection.Count > 0)
            {
                cboSearch.SelectedIndex = 0;
            }

            if (Properties.Settings.Default.ReplaceCollection.Count > 0)
            {
                cboReplace.SelectedIndex = 0;
            }

            chkArtist.Checked = Properties.Settings.Default.chkArtist;
            chkAlbumArtist.Checked = Properties.Settings.Default.chkAlbumArtist;
            chkAlbum.Checked = Properties.Settings.Default.chkAlbum;
            chkTitle.Checked = Properties.Settings.Default.chkTitle;
            chkIgnoreCase.Checked = Properties.Settings.Default.chkIgnoreCase;
        }

        private void FormTitleReplace_FormClosing(object sender, FormClosingEventArgs e)
        {
            SaveComboBoxes();

            Properties.Settings.Default.SearchCollection = _searchCollection;
            Properties.Settings.Default.ReplaceCollection = _replaceCollection;
            Properties.Settings.Default.chkArtist = chkArtist.Checked;
            Properties.Settings.Default.chkAlbumArtist = chkAlbumArtist.Checked;
            Properties.Settings.Default.chkAlbum = chkAlbum.Checked;
            Properties.Settings.Default.chkTitle = chkTitle.Checked;
            Properties.Settings.Default.chkIgnoreCase = chkIgnoreCase.Checked;

            Properties.Settings.Default.Save();
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            _searchInTag = cboSearch.Text;
            _replaceInTag = cboReplace.Text;
            _ignoreCase = chkIgnoreCase.Checked;

            if (chkArtist.Checked)
            {
                _tags.Add("ARTIST");
            }

            if (chkAlbumArtist.Checked)
            {
                _tags.Add("ALBUMARTIST");
            }

            if (chkAlbum.Checked)
            {
                _tags.Add("ALBUM");
            }

            if (chkTitle.Checked)
            {
                _tags.Add("TITLE");
            }
        }
    }
}
